﻿//Xóa dữ liệu ở các thẻ đã chứa dữ liệu
function ClearData(strObj) {
    var arr = strObj.split(",");
    if (arr != null) {
        for (var i = 0; i < arr.length; i++) {
            arr[i] = $.trim(arr[i]);
            var obj = $("[id$=" + arr[i] + "]");
            if (obj != null) {
                if (arr[i].indexOf("txt") == 0) {
                    obj.val("");
                }
                else if (arr[i].indexOf("chk") == 0) {
                    obj.prop("checked", "");
                }
                else if (arr[i].indexOf("ddl") == 0) {
                    $("option[value='']", obj).prop("selected", true);
                }
                else if(arr[i].indexOf("hdf") == 0){
                    obj.val("AddNew");
                }
            }
        }
    }
}

//Đăng ký jquery UI Dialog
function RegisterDialog() {
    $(".dialog-form").dialog({
        autoOpen: false,
        //height: 300,
        //width: 350,
        modal: true
    });
    $(".dialog-form").parent().appendTo($("form:first"))
}

//Mở dialog
function OpenDialog(dialogId, _width, _height) {
    if (_width != null && _height != null) {
        $("#" + dialogId).dialog({
            autoOpen: false,
            width: _width,
            height: _height,
            modal: true,
        });
    }
    else if(_width == null && _height == null) {
        $("#" + dialogId).dialog({
            autoOpen: false,
            width: 450,
            height: 678,
            modal: true,
        });
    }
    else if(_width != null && _height == null){
        $("#" + dialogId).dialog({
            autoOpen: false,
            width: _width,
            modal: true,
        });
    }
    else if(_width == null && _height != null){
        $("#" + dialogId).dialog({
            autoOpen: false,
            height: _height,
            modal: true,
        });
    }
    $("#" + dialogId).dialog("open");
    return false;
}

//Active menu
function ActiveMenu(){
    var functionId = $("#formId").val();
    if (functionId != null) {
        $("#mainNav ul li").removeClass("active");
        $("#mainNav ul li#" + functionId).addClass("active");
    }
}

//Tắt tự động điền form (autocomplete) của textbox
function TurnOfAutoComplete(){
    var input = $("input[type=text]");
    if (input != null) 
        $(input).prop("autocomplete", "off");
}

//Check toàn bộ các checkbok của table
function CheckAllItem(checkbox, table){
    $(":checkbox", $("[id$=" + table + "]")).prop("checked", checkbox.checked);
}

//Datetime picker
function DateTimePicker(strObj){
    var arr = strObj.split(",");
    if (arr != null) {
        for (var i = 0; i < arr.length; i++) {
            arr[i] = $.trim(arr[i]);
            var obj = $("[id$=" + arr[i] + "]");
            if (obj != null) {
                obj.datetimepicker({
	                addSliderAccess: true,
	                sliderAccessArgs: { touchonly: false }
                });
            }
        }
    }
}

//time picker
function TimePicker(strObj){
    var arr = strObj.split(",");
    if (arr != null) {
        for (var i = 0; i < arr.length; i++) {
            arr[i] = $.trim(arr[i]);
            var obj = $("[id$=" + arr[i] + "]");
            if (obj != null) {
                obj.timepicker({
	                hourGrid: 4,
	                minuteGrid: 10
                });
            }
        }
    }
}

//Date picker
function DatePicker(strObj){
    var arr = strObj.split(",");
    if (arr != null) {
        for (var i = 0; i < arr.length; i++) {
            arr[i] = $.trim(arr[i]);
            var obj = $("[id$=" + arr[i] + "]");
            if (obj != null) {
                obj.datepicker();
            }
        }
    }
}

//Validate control theo điều kiện numcheck
function ValidateKeypress(numcheck, e) {
    var keynum, keychar, numcheck;
    if (window.event) {//IE
        keynum = e.keyCode;
    }
    else if (e.which) {// Netscape/Firefox/Opera
        keynum = e.which;
    }
    if (keynum == 8 || keynum == 127 || keynum == null || keynum == 9 || keynum == 0 || keynum == 13) return true;
    keychar = String.fromCharCode(keynum);
    return numcheck.test(keychar);
}